<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<ng-container *ngIf="groupsTableData">
  <ng-container *ngFor="let group of groupsTableData; let i = index">
    <mat-card class="endpoint-group-card">
      <mat-card-content>
        <div class="endpoint-group-card__header">
          <div class="endpoint-group-card__header__title">
            <span class="mat-h4">{{ group?.name }}</span>
            <span *ngIf="plugins?.get(group.type)" class="gio-badge-primary">
              <mat-icon [svgIcon]="plugins?.get(group.type)?.icon"></mat-icon>
              {{ plugins?.get(group.type)?.name }}
            </span>
            <span class="gio-badge-accent">
              <mat-icon svgIcon="gio:share-2"></mat-icon>
              {{ group.loadBalancerType }}
            </span>
            <span
              class="gio-badge-neutral"
              *ngIf="i === 0"
              [matTooltip]="'The default group of endpoints used by the API is the first one listed'"
            >
              Default
            </span>
          </div>

          <div class="endpoint-group-card__header__actions">
            <button
              id="{{ 'moveUpBtn-' + i }}"
              *ngIf="groupsTableData?.length > 1 && i !== 0"
              mat-stroked-button
              class="endpoint-group-card__header__actions__button"
              aria-label="Reorder element to a higher position"
              (click)="reorderEndpointGroup(i, i - 1)"
              [disabled]="isReadOnly || isReordering"
            >
              <mat-icon svgIcon="gio:arrow-up"></mat-icon>
            </button>
            <button
              id="{{ 'moveDownBtn-' + i }}"
              *ngIf="groupsTableData?.length > 1 && i !== groupsTableData.length - 1"
              mat-stroked-button
              class="endpoint-group-card__header__actions__button"
              aria-label="Reorder element to a lower position"
              (click)="reorderEndpointGroup(i + 1, i)"
              [disabled]="isReadOnly || isReordering"
            >
              <mat-icon svgIcon="gio:arrow-down"></mat-icon>
            </button>
            <button
              *gioPermission="{ anyOf: ['api-definition-u'] }"
              mat-stroked-button
              [attr.aria-label]="isReadOnly ? 'View endpoint group' : 'Edit endpoint group'"
              [routerLink]="['./', i]"
            >
              <mat-icon [svgIcon]="isReadOnly ? 'gio:eye-empty' : 'gio:edit-pencil'"></mat-icon>{{ isReadOnly ? 'View' : 'Edit' }}
            </button>
            <ng-container *gioPermission="{ anyOf: ['api-definition-u'] }">
              <button
                *ngIf="groupsTableData?.length > 1"
                mat-stroked-button
                aria-label="Delete endpoints group"
                (click)="deleteGroup(group.name)"
                [disabled]="isReadOnly"
              >
                <mat-icon svgIcon="gio:trash"></mat-icon>
                Delete
              </button>
            </ng-container>
          </div>
        </div>
        <table
          mat-table
          [dataSource]="group.endpoints"
          [attr.id]="'groupsTable-' + i"
          aria-label="Endpoint group table"
          class="gio-table-light"
        >
          <!-- Name Column -->
          <ng-container matColumnDef="name">
            <th mat-header-cell *matHeaderCellDef id="name">Name</th>
            <td mat-cell *matCellDef="let element">
              {{ element.name }}
            </td>
          </ng-container>

          <!-- Options Column -->
          <ng-container matColumnDef="options">
            <th mat-header-cell *matHeaderCellDef id="options">Options</th>
            <td mat-cell *matCellDef="let element">
              <span class="neutral" *ngIf="element.options.healthCheck"> Health-check </span>
            </td>
          </ng-container>

          <!-- Weight Column -->
          <ng-container matColumnDef="weight">
            <th mat-header-cell *matHeaderCellDef id="weight">Weight</th>
            <td mat-cell *matCellDef="let element">
              {{ element.weight }}
            </td>
          </ng-container>

          <!-- Actions Column -->
          <ng-container matColumnDef="actions">
            <th mat-header-cell *matHeaderCellDef id="actions">Actions</th>
            <td mat-cell *matCellDef="let element; let j = index">
              <div class="endpoint-group-card__table__actions" *gioPermission="{ anyOf: ['api-definition-u'] }">
                <a
                  mat-icon-button
                  [attr.aria-label]="isReadOnly ? 'View endpoint' : 'Edit endpoint'"
                  [matTooltip]="isReadOnly ? 'View endpoint' : 'Edit endpoint'"
                  [routerLink]="['./', i, j]"
                >
                  <mat-icon [svgIcon]="isReadOnly ? 'gio:eye-empty' : 'gio:edit-pencil'"></mat-icon>
                </a>
                <button
                  mat-icon-button
                  aria-label="Delete endpoint"
                  (click)="deleteEndpoint(group.name, element.name)"
                  [disabled]="isReadOnly || (groupsTableData?.length === 1 && group.endpoints.length === 1)"
                >
                  <mat-icon
                    [matTooltip]="
                      groupsTableData?.length === 1 && group.endpoints.length === 1
                        ? 'At least one endpoint is required'
                        : 'Delete endpoint'
                    "
                    svgIcon="gio:trash"
                  ></mat-icon>
                </button>
              </div>
            </td>
          </ng-container>

          <tr mat-header-row *matHeaderRowDef="endpointsDisplayedColumns"></tr>
          <tr mat-row *matRowDef="let row; columns: endpointsDisplayedColumns"></tr>

          <!-- Row shown when there is no data -->
          <tr class="mat-mdc-row mdc-data-table__row" *matNoDataRow>
            <td class="mat-mdc-cell mdc-data-table__cell" [attr.colspan]="endpointsDisplayedColumns.length">No Endpoints</td>
          </tr>
        </table>
        <a
          *gioPermission="{ anyOf: ['api-definition-u'] }"
          mat-stroked-button
          aria-label="Add endpoint"
          class="add_endpoint"
          [routerLink]="['./', i, 'new']"
          [disabled]="isReadOnly"
          >Add endpoint</a
        >
      </mat-card-content>
    </mat-card>
  </ng-container>
</ng-container>

<button
  *gioPermission="{ anyOf: ['api-definition-u'] }"
  mat-flat-button
  color="primary"
  aria-label="Add endpoint group"
  [routerLink]="['./', 'new']"
  [disabled]="isReadOnly"
>
  Add endpoint group
</button>

<div *ngIf="shouldUpgrade">
  <gio-license-banner [license]="license$ | async" [isOEM]="isOEM$ | async" (onRequestUpgrade)="onRequestUpgrade()"></gio-license-banner>
</div>
